package star.crust.cartspace.framework;

import javax.vecmath.Matrix3d;

import star.core.util.geometry.DoubleXYZ;
import star.core.util.geometry.HTransform;
import star.core.util.geometry.POTransform;
import star.core.util.geometry.Quaternion;


public class CrtBody {
	private String id = "";
	private CrtMesh geometry = null;
	private HTransform position = null;
	
	public String getID() {
		return id;
	}

	public HTransform getPosition() {
		return position;
	}

	public void setGeometry(CrtMesh geometry) {
		this.geometry = geometry;
	}

	public CrtMesh getGeometry() {
		return geometry;
	}

	public String toString() {
		String doc = "<CrtRigidBody " + id + " " + ">";
		return doc;
	}

	// Per Solid/Rapid
	private CrtBoundingTree boundingtree = null;
	private POTransform POposition = null;

	/*
	public CrtBody(String id) {
		this.id = id;
		position = new HTransform();
	}
	*/
	public CrtBody(String id) {
		this.id = id;
		position = new HTransform();
		POposition = new POTransform(null);
	}

	public CrtBoundingTree getBoundingTree() {
		return this.boundingtree;
	}

	public POTransform getPOPosition() {
		return POposition;
	}

	/**
	 * Il metodo verifica l'intersezione tra 
	 * due gerarchie di tipo AABB
	 * @param b
	 * @param t
	 * @return
	 */
	public static boolean intersect(CrtBody a,CrtBody b)
	{
		POTransform t= new POTransform(new DoubleXYZ(0,0,0));
		t.setInvMult(a.POposition, b.POposition);
		Matrix3d m=t.absolute();
		return CrtBoundingTree.intersect(a.getBoundingTree(),b.getBoundingTree(),t,m);
	}

	public void setOrientation(Quaternion q)
	{
		this.POposition.setOrientation(q);
	}

	public void setPosition(DoubleXYZ pos)
	{
		this.POposition.setPosition(pos);
	}

	/**
	 * Il metodo si occupa di selezionare
	 * la procedura di creazione della gerarchia
	 * a seconda del tipo di volume.
	 * @param type
	 */
	public void updateBoundingTree(String type) {
		if (this.boundingtree == null) {
			if (type == "OBB") {
				float[][] pR  = {{(float) 0.0, (float) 0.0, (float) 0.0},{(float) 0.0, (float) 0.0, (float) 0.0},{(float) 0.0, (float) 0.0, (float) 0.0}};
				float[] pT  = {(float) 0.0, (float) 0.0, (float) 0.0};
				this.boundingtree = new CrtBoundingTree(new CrtObb(pR, pT,this.geometry.meshePrimitives));
			}else
			{
				this.boundingtree=new CrtBoundingTree(new CrtAabb(this.geometry));
			}		
		}
	}

	public void setPosition(HTransform ht)
	{
		this.position=ht;
	}
}
